package com.tjp.eight.ui.fragment.mymsg.receicezan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

/**
 * Created by Administrator on 2017/2/27.
 */
public class FragmentReceiveZan extends BaseFragment<ReceiveZanPresenter> implements ReceiveZanView {
    private RecyclerView rv_receive_zan;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private ReceiveZanAdapter receiveZanAdapter;
    private TextView tv_no_data;
    private int page = 1;

    @Override
    protected ReceiveZanPresenter createPresenter() {
        return new ReceiveZanPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_receive_zan, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson());
    }

    private String getPJson() {
        ZanPModel model = new ZanPModel();
        model.page = page;
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        page = page + 1;
        return new Gson().toJson(model);
    }

    void initViews() {
        tv_no_data = (TextView) contentView.findViewById(R.id.tv_no_data);
        rv_receive_zan = (RecyclerView) contentView.findViewById(R.id.rv_receive_zan);
        rv_receive_zan.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        receiveZanAdapter = new ReceiveZanAdapter(getActivity());
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(receiveZanAdapter);

        rv_receive_zan.addOnScrollListener(new EndlessRecyclerOnScrollListener(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)) {
            @Override
            public void onLoadMore(int currentPage) {
                presenter.loadData(getPJson());
            }
        });
        rv_receive_zan.setAdapter(headerViewRecyclerAdapter);
    }

    @Override
    public void loadSuccess(ZanRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                if (model.data != null && !model.data.isEmpty()) {
                    receiveZanAdapter.addData(model.data);
                }
                if (receiveZanAdapter.getItemCount() == 0) {
                    tv_no_data.setVisibility(View.VISIBLE);
                } else {
                    tv_no_data.setVisibility(View.GONE);
                }
            } else {
                ToatUtils.toastSuccess(getContext(), model.msg);
            }
        }
    }

    @Override
    public void fail(String msg) {

    }
}
